#!/bin/bash
#
# Copyright (c) 2016-2020 The Brenwill Workshop Ltd.
#
# fetchDependencies - Retrieves the correct versions of all dependencies
#
# macOS usage: ./fetchDependencies [--ios] [--iosfat] [--tvos] [--tvosfat] [--macos] [--all] [--none]
#                                  [-v] [--debug] [--build-spirv-tools]
#                                  [--v-headers-root path] [--spirv-cross-root path] [--glslang-root path]
#
#      --ios
#              Build the external libraries for the iOS platform.
#
#      --iosfat
#              Build a single binary for each external library, each containing code
#              for both iOS and iOS Simulator platforms.
#
#      --tvos
#              Build the external libraries for the tvOS platform.
#
#      --tvosfat
#              Build a single binary for each external library, each containing code
#              for both tvOS and tvOS Simulator platforms.
#
#      --macos
#              Build the external libraries for the macOS platform.
#
#      --all
#              Equivalent to specifying [--iosfat --tvosfat --macos]. Results in thee
#              binaries for each external library: one for the iOS and iOS Simulator platforms,
#              one for the tvOS and tvOS Simulator platforms, and one for the macOS platform.
#
#      --none
#              Don't build the external libraries for any platform.
#
#      Multiple platform options may be specified. At least one platform option must be specified.
#
#      --debug
#              Build the external libraries in Debug mode, which may be useful when debugging
#              and tracing calls into those libraries.
#
#      --no-parallel-build
#              Build the external libraries serially instead of in parallel using background processes.
#
#      --glslang-root path
#              "path" specifies a directory path to a KhronosGroup/glslang repository.
#              This repository does need to be built and the build directory must be in the
#              specified directory. It should be built the same way this script builds it.
#
#      --build-spirv-tools
#              Build the full spirv-tools distribution. Normally this is not needed, because
#              MoltenVK includes a template of pre-generated SPIRV-Tools header files, which
#              is all that is needed. Avoiding the spirv-tools build saves significant time
#              during the running of this script, and is necessary during CI because Travis CI
#              cannot support the required use of Python3 by the spirv-tools build. This flag
#              is used by the packagePregenSpirvToolsHeaders script which regenerates the
#              spirv-tools header files and repackages the Templates/spirv-tools/build.zip
#              file when the spirv-tools library version is upgraded.
#
#      --spirv-cross-root path
#              "path" specifies a directory path to a KhronosGroup/SPIRV-Cross repository.
#              This repository does not have to be built.
#
#      -v      verbose output
#
#      --v-headers-root path
#              "path" specifies a directory path to a KhronosGroup/Vulkan-Headers repository.
#              This repository does not have to be built.
#

set -e

# ----------------- Functions -------------------

BLD_NONE=""
BLD_IOS=""
BLD_IOS_SIM=""
BLD_TVOS=""
BLD_TVOS_SIM=""
BLD_MACOS=""
BLD_SPECIFIED=""
XC_CONFIG="Release"
XC_BUILD_VERBOSITY="-quiet"
XC_USE_BCKGND="Y"
V_HEADERS_ROOT=""
SPIRV_CROSS_ROOT=""
GLSLANG_ROOT=""
BLD_SPV_TLS=""

while (( "$#" )); do
  case "$1" in
       --ios)
         BLD_IOS="Y"
         BLD_SPECIFIED="Y"
         shift 1
         ;;
       --iosfat)
         BLD_IOS="Y"
         BLD_IOS_SIM="Y"
         BLD_SPECIFIED="Y"
         shift 1
         ;;
       --tvos)
         BLD_TVOS="Y"
         BLD_SPECIFIED="Y"
         shift 1
         ;;
       --tvosfat)
         BLD_TVOS="Y"
         BLD_TVOS_SIM="Y"
         BLD_SPECIFIED="Y"
         shift 1
         ;;
       --macos)
         BLD_MACOS="Y"
         BLD_SPECIFIED="Y"
         shift 1
         ;;
       --all)
         BLD_IOS="Y"
         BLD_IOS_SIM="Y"
         BLD_TVOS="Y"
         BLD_TVOS_SIM="Y"
         BLD_MACOS="Y"
         BLD_SPECIFIED="Y"
         shift 1
         ;;
       --none)
         BLD_NONE="Y"
         BLD_SPECIFIED="Y"
         shift 1
         ;;
       --debug)
         XC_CONFIG="Debug"
         shift 1
         ;;
       --no-parallel-build)
         XC_USE_BCKGND=""
         shift 1
         ;;
       -v)
         XC_BUILD_VERBOSITY=""
         shift 1
         ;;
       --build-spirv-tools)
         BLD_SPV_TLS="Y"
         shift 1
         ;;
       --skip-spirv-tools-build)	#deprecated
         BLD_SPV_TLS=""
         shift 1
         ;;
       --v-headers-root)
         V_HEADERS_ROOT=$2
         shift 2
         ;;
       --spirv-cross-root)
         SPIRV_CROSS_ROOT=$2
         shift 2
         ;;
       --glslang-root)
         GLSLANG_ROOT=$2
         shift 2
         ;;
       -*|--*=)
         echo "Error: Unsupported option $1" >&2
         exit 1
         ;;
  esac
done

# Update a repository. If it exists, fetch it; if not, clone it.
# $1 repo name
# $2 repo url
# $3 repo revision (commit SHA)
function update_repo() {
	echo "$1 repo: $2"
	echo "$1 revision: $3"

	if [ -d $1 -a -d $1/.git ]; then
		cd $1
		git fetch --all
		git checkout --force $3
		cd -  > /dev/null
	else
		rm -rf $1
		git clone $2 $1
		cd $1
		git checkout $3
		cd -  > /dev/null
	fi
}

# Build a repository
# $1 repo name
function build_repo() {
	echo "Building $1"

	mkdir -p $1/build
	cd $1/build
	if type ninja >/dev/null 2>&1 ; then
		cmake -G Ninja -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=install ..
		ninja
	else
		cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=install ..
		make -j $(sysctl -n hw.activecpu)
	fi

	cd -  > /dev/null
}


# ----------------- Main -------------------

EXT_DIR=External
EXT_REV_DIR=../ExternalRevisions

echo
echo ========== Retrieving MoltenVK dependencies into ${EXT_DIR} ==========

mkdir -p ${EXT_DIR}
cd ${EXT_DIR}


# ----------------- Cereal -------------------

echo
echo ========== Cereal ==========
echo

REPO_NAME=cereal
REPO_URL="https://github.com/USCiLab/${REPO_NAME}.git"
REPO_REV=$(cat "${EXT_REV_DIR}/${REPO_NAME}_repo_revision")

update_repo ${REPO_NAME} ${REPO_URL} ${REPO_REV}


# ----------------- Vulkan-Headers -------------------

echo
echo ========== Vulkan-Headers ==========
echo

# When MoltenVK is built by something that already has
# a copy of this repo, use it by creating a symlink.

REPO_NAME=Vulkan-Headers

if [ ! "$V_HEADERS_ROOT" = "" ]; then
	rm -rf ${REPO_NAME}
	ln -sfn ${V_HEADERS_ROOT} ${REPO_NAME}
else
	REPO_URL="https://github.com/KhronosGroup/${REPO_NAME}.git"
	REPO_REV=$(cat "${EXT_REV_DIR}/${REPO_NAME}_repo_revision")

	update_repo ${REPO_NAME} ${REPO_URL} ${REPO_REV}
fi

# ----------------- Vulkan-Portability -------------------

echo
echo ========== Vulkan-Portability ==========
echo

REPO_NAME=Vulkan-Portability
REPO_URL="https://github.com/KhronosGroup/${REPO_NAME}.git"
REPO_REV=$(cat "${EXT_REV_DIR}/${REPO_NAME}_repo_revision")

update_repo ${REPO_NAME} ${REPO_URL} ${REPO_REV}


# ----------------- SPIRV-Cross -------------------

echo
echo ========== SPIRV-Cross ==========
echo

# When MoltenVK is built by something that already has
# a copy of this repo, use it by creating a symlink.

REPO_NAME=SPIRV-Cross

if [ ! "$SPIRV_CROSS_ROOT" = "" ]; then
	rm -rf ${REPO_NAME}
	ln -sfn ${SPIRV_CROSS_ROOT} ${REPO_NAME}
else
	REPO_URL="https://github.com/KhronosGroup/${REPO_NAME}.git"
	REPO_REV=$(cat "${EXT_REV_DIR}/${REPO_NAME}_repo_revision")

	update_repo ${REPO_NAME} ${REPO_URL} ${REPO_REV}
fi



# ----------------- glslang -------------------

echo
echo ========== glslang and SPIRV-Tools ==========
echo

# When MoltenVK is built by something that already has
# a copy of this repo, use it by creating a symlink.

REPO_NAME=glslang

if [ ! "$GLSLANG_ROOT" = "" ]; then
	rm -rf ${REPO_NAME}
	ln -sfn ${GLSLANG_ROOT} ${REPO_NAME}
else
	REPO_URL="https://github.com/KhronosGroup/${REPO_NAME}.git"
	REPO_REV=$(cat "${EXT_REV_DIR}/${REPO_NAME}_repo_revision")

	update_repo ${REPO_NAME} ${REPO_URL} ${REPO_REV}

	cd ${REPO_NAME}
	./update_glslang_sources.py
	rm -rf build
	./build_info.py .        \
		-i build_info.h.tmpl  \
		-o build/include/glslang/build_info.h
	cd -  > /dev/null
fi

# Build the embedded spirv-tools, or use option of pre-generated headers
SPV_TLS_DIR="${REPO_NAME}/External/spirv-tools"
if [ "$BLD_SPV_TLS" = "Y" ]; then
	build_repo "${SPV_TLS_DIR}"
else
	unzip -o -q -d "${SPV_TLS_DIR}" ../Templates/spirv-tools/build.zip
	rm -rf "${SPV_TLS_DIR}/__MACOSX"
fi



# ----------------- Vulkan-Tools -------------------

echo
echo ========== Vulkan-Tools ==========
echo

REPO_NAME=Vulkan-Tools
REPO_URL="https://github.com/KhronosGroup/${REPO_NAME}.git"
REPO_REV=$(cat "${EXT_REV_DIR}/${REPO_NAME}_repo_revision")

update_repo ${REPO_NAME} ${REPO_URL} ${REPO_REV}


# ----------------- VulkanSamples -------------------

echo
echo ========== VulkanSamples ==========
echo

REPO_NAME=VulkanSamples
REPO_URL="https://github.com/LunarG/${REPO_NAME}.git"
REPO_REV=$(cat "${EXT_REV_DIR}/${REPO_NAME}_repo_revision")

update_repo ${REPO_NAME} ${REPO_URL} ${REPO_REV}

# Generate dispatch files needed for Hologram demo
cd "${REPO_NAME}/Sample-Programs/Hologram"
./generate-dispatch-table HelpersDispatchTable.h
./generate-dispatch-table HelpersDispatchTable.cpp
cd -  > /dev/null


# ----------------- Cleanup -------------------

cd ..


# -------------- Build MoltenVK external library dependencies -----------------

# if no platform was specified, print usage message and exit
if [ "$BLD_SPECIFIED" = "" ]; then
	echo "ERROR: You did not specify a platform to build."
	echo "Include one or more of the following platform options:"
	echo "    --ios --iosfat --tvos --tvosfat --macos --all"
	echo "See the instructions in the fetchDependencies script for more info."
	exit 1
fi

echo
echo ========== Started building dependency libraries at `date +"%r"` ==========
echo Please be patient on first build

# Build an Xcode scheme for an OS and platform
# 1 - OS
# 2 - platform
function build_impl() {
	XC_OS=${1}
	XC_PLTFM=${2}

	XC_SCHEME="${EXT_DEPS}-${XC_OS}"
	XC_LCL_DD_PATH="${XC_DD_PATH}/Intermediates/${XC_OS}"
	XC_DEST="generic/platform=${XC_PLTFM}"

	echo Building external libraries for ${XC_PLTFM}

	xcodebuild 										\
		-project "${XC_PROJ}"						\
		-scheme "${XC_SCHEME}"						\
		-destination "${XC_DEST}"					\
		-configuration "${XC_CONFIG}"				\
		-derivedDataPath "${XC_LCL_DD_PATH}"		\
		${XC_BUILD_VERBOSITY}						\
		build

	echo Completed building external libraries for ${XC_PLTFM}
}

# Select whether or not to run the build in parallel.
# 1 - OS
# 2 - platform
function build() {
	if [ "$XC_USE_BCKGND" != "" ]; then
		build_impl ${1} ${2} &
	else
		build_impl ${1} ${2}
	fi
}

# Build a fat library for the OS containing code for the OS and its Simulator
# Builds serially to ensure both are built
# 1 - OS
function build_fat_impl() {
	XC_OS=${1}

	build_impl ${XC_OS} ${XC_OS}
	build_impl ${XC_OS} "${XC_OS} Simulator"
}

# Select whether or not to run the build in parallel.
# 1 - OS
function build_fat() {
	if [ "$XC_USE_BCKGND" != "" ]; then
		build_fat_impl ${1} &
	else
		build_fat_impl ${1}
	fi
}

EXT_DEPS=ExternalDependencies
XC_PROJ="${EXT_DEPS}.xcodeproj"
XC_DD_PATH="${EXT_DIR}/build"

# Structure build tasks by platform so they can be built in parallel per platform.
# Content for each platform must be built in series to avoid
if [ "$XC_USE_BCKGND" != "" ]; then
	trap "exit" INT TERM ERR
	trap "kill 0" EXIT
fi

if [ "$BLD_NONE" != "" ]; then
	echo Not building any platforms
fi

if [ "$BLD_MACOS" != "" ]; then
	build "macOS" "macOS"
fi

if [ "$BLD_IOS" != "" ]; then
	if [ "$BLD_IOS_SIM" != "" ]; then
		build_fat "iOS"
	else
		build "iOS" "iOS"
	fi
fi

if [ "$BLD_TVOS" != "" ]; then
	if [ "$BLD_TVOS_SIM" != "" ]; then
		build_fat "tvOS"
	else
		build "tvOS" "tvOS"
	fi
fi

# Wait for any background process (if selected) to finish
if [ "$XC_USE_BCKGND" != "" ]; then
	wait
fi

# Update latest symlink and clean MoltenVK for rebuild
PROJECT_DIR="."
CONFIGURATION=${XC_CONFIG}
. "./Scripts/package_ext_libs_finish.sh"

echo ========== Finished at `date +"%r"` ==========
exit 0
